/**
 * 演示程序当前的 “注册/登录” 等操作，是基于 “本地存储” 完成的
 * 当您要参考这个演示程序进行相关 app 的开发时，
 * 请注意将相关方法调整成 “基于服务端Service” 的实现。
 **/
(function($, owner) {
	/*
	 * 获取服务器配置信息
	 */
	owner.getConfigInfo = function() {
		var configInfo = JSON.parse(localStorage.getItem('configinfo')) || "";
		return configInfo;
	};

	/*
	 * 设置服务器配置信息
	 */
	owner.setConfigInfo = function(info) {
		localStorage.setItem('configinfo', JSON.stringify(info));
	};

	/*
	 * 获取登录用户信息
	 */
	owner.getLoginInfo = function() {
		var userLoginInfo = JSON.parse(localStorage.getItem('logininfo')) || "";
		return userLoginInfo;
	};

	/*
	 * 获取用户信息userinfo
	 */
	owner.getUserInfo = function() {
		var getuserInfo = JSON.parse(localStorage.getItem('userinfo')) || "{}";
		return getuserInfo;
	};

	/*
	 * 设置用户信息userinfo
	 */
	/*owner.setUserInfo = function(num) {
		var userobj = {
			"mobile": num
		}
		console.log('userobj:' + JSON.stringify(userobj));
		//	获取当前用户登录数据
		mui.get(config.getUserInfo, userobj, function(r) {
			console.log('r:' +JSON.stringify(r));
			if(r.errCode == 0) {
				localStorage.setItem('userinfo', JSON.stringify(r.data));
			}
		});

	};*/

	/*
	 * 获取用通讯录存储数据
	 */
	owner.getContactsInfo = function() {
		var getContactsInfo = JSON.parse(localStorage.getItem('contacts')) || "{}";
		return getContactsInfo;
	};

	/*
	 * 设置通讯录存储
	 */
	owner.setContactsInfo = function() {
		mui.get(config.getContactsInfo, function(r) {
			if(r.errCode == 0) {
				localStorage.setItem('contacts', JSON.stringify(r.data));
				return;
			}
			mui.toast(e.errMsg);
		});

	};

	/*
	 * 获取指定用户
	 */
	owner.getUserBy = function(id) {
		var str = localStorage.getItem('contacts');
		if(!str || str == '') return null;
		var json = JSON.parse(str);
		for(var i = 0; i < json.length; i++) {
			if(json[i].id == id) {
				return json[i];
			}
		}
		return null;
	};

	/*
	 * 	强制跳转登陆界面
	 */
	owner.getPageJump = function() {
		mui.openWindow({
			url: 'login.html',
			id: 'login',
			createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
			show: {
				autoShow: true, //页面loaded事件发生后自动显示，默认为true
				aniShow: 'zoom-fade-out', //页面显示动画，默认为”slide-in-right“；
				duration: 300 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
			},
			waiting: {
				autoShow: true //自动显示等待框，默认为true
			}
		})
	};

	/**
	 * 找回密码
	 **/
	owner.forgetPassword = function(email, callback) {
		callback = callback || $.noop;
		if(!checkEmail(email)) {
			return callback('邮箱地址不合法');
		}
		return callback(null, '新的随机密码已经发送到您的邮箱，请查收邮件。');
	};

	/**
	 * 设置应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 获取应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return settingsText;
	}

	/**
	 * 设置NVR配置
	 **/
	owner.setNvrConfig = function() {
		mui.get(config.nvrConfig, function(r) {
			if(r) {
				localStorage.setItem('nvrconfig', JSON.stringify(r));
			}
		});
	}

	/**
	 * 获取NVR配置 
	 **/
	owner.getNvrConfig = function() {
		var strNvrConfig = localStorage.getItem('nvrconfig');
		return JSON.parse(strNvrConfig);
	}

	/**
	 * 获取本地是否安装客户端
	 **/
	owner.isInstalled = function(id) {
		if(id === 'qihoo' && mui.os.plus) {
			return true;
		}
		if(mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var packageManager = main.getPackageManager();
			var PackageManager = plus.android.importClass(packageManager)
			var packageName = {
				"qq": "com.tencent.mobileqq",
				"weixin": "com.tencent.mm",
				"sinaweibo": "com.sina.weibo"
			}
			try {
				return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
			} catch(e) {}
		} else {
			switch(id) {
				case "qq":
					var TencentOAuth = plus.ios.import("TencentOAuth");
					return TencentOAuth.iphoneQQInstalled();
				case "weixin":
					var WXApi = plus.ios.import("WXApi");
					return WXApi.isWXAppInstalled()
				case "sinaweibo":
					var SinaAPI = plus.ios.import("WeiboSDK");
					return SinaAPI.isWeiboAppInstalled()
				default:
					break;
			}
		}
	}

	
	/*  // 获取用户头像
    owner.getHeadImg = function() {
        var headImg = owner.getUserInfo().my_icon || "";
        return headImg;
    }

    //设置用户头像
    owner.setHeadImg = function(baseData) {
        var userInfo = owner.getUserInfo();
        userInfo.my_icon = baseData; //只对my_icon这一项进行修改，其他不动
        owner.setUserInfo(userInfo);
    }*/
    

}(mui, window.app = {}));